package com.ssh.manager.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ssh.common.util.Page;
import com.ssh.dao.IDao;
import com.ssh.entity.ActivityImage;
import com.ssh.entity.TaskBrand;
import com.ssh.entity.TaskProductEmployee;
import com.ssh.manager.TaskBrandManager;

@Service
public class TaskBrandManagerImpl implements TaskBrandManager {
	@Resource
	private IDao daoImpl;

	@Override
	public TaskBrand saveUpdata(TaskBrand taskBrand) {
		return daoImpl.saveOrUpdateDomain(taskBrand);
	}

	@Override
	public TaskBrand getById(Long id) {
		return daoImpl.getDomain(TaskBrand.class, id);
	}

	@Override
	public void findList(Map conditionMap, Page page, String isDesc) {
		String sql = "select count(*) from TaskBrand where del!=1";
		String findSql = "from TaskBrand where del!=1";
		if (conditionMap.get("task")!=null && !conditionMap.get("task").equals("")) {
			sql += " and task.content like '%"+conditionMap.get("task")+"%'";
			findSql += " and task.content like '%"+conditionMap.get("task")+"%'";
		}
		if (conditionMap.get("brand")!=null && !conditionMap.get("brand").equals("")) {
			sql += " and brand.id"+conditionMap.get("brand");
			findSql += " and brand.id"+conditionMap.get("brand");
		}
		int sum = daoImpl.countAll(sql, null);
		page.setTotalCount(new Long(sum));
		page.countTotalPageNum();
		
		findSql += " order by createDate "+isDesc;
		List<TaskBrand> taskBrands = (List<TaskBrand>)daoImpl.findListBySql(findSql, new Object[]{}, false, true, false, page.getNumberPerPage()*(page.getCurPageNum()-1), page.getNumberPerPage());
		page.setPageData(taskBrands);
	}

	@Override
	public boolean delete(Long id) {
		try {
			TaskBrand taskBrand = daoImpl.getDomain(TaskBrand.class, id);
			String sql = "from TaskProductEmployee where del!=1 and taskBrand.id=?";
			List<TaskProductEmployee> taskProductEmployees = (List<TaskProductEmployee>)daoImpl.findListBySql(sql, new Object[]{taskBrand.getId()}, false, true, false, -1, -1);
			for (int i = 0; i < taskProductEmployees.size(); i++) {
				taskProductEmployees.get(i).setDel(1);
				daoImpl.saveOrUpdateDomain(taskProductEmployees.get(i));
			}
			taskBrand.setDel(1);
			daoImpl.saveOrUpdateDomain(taskBrand);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
}
